using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TJ_MainNameSpace
{
    public partial class Form_k82 : Form
    {
        public Form_k82()
        {
            InitializeComponent();
        }
        private kongjian82.Shouche10kv_Down myshouche;
        public Form_k82(kongjian82.Shouche10kv_Down id)
        {
            InitializeComponent();
            this.myshouche = id;

        }

        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);
        private void Form_k82_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = myshouche.Name;
            this.textBox2.Text = myshouche.Bol;
            this.textBox3.Text = myshouche.FaultSignal;


            con.Open();
            //MySqlCommand setchar = new MySqlCommand(@"set names gb2312", con);
            //setchar.ExecuteNonQuery();

            string sql1 = "select distinct db_areaname from dbpoint where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";

            MySqlDataAdapter oda1 = new MySqlDataAdapter(sql1, con);

            DataSet ds3 = new DataSet();

            oda1.Fill(ds3, "dbpoint");

            this.comboBox1.DataSource = ds3.Tables[0];
            this.comboBox1.DisplayMember = "db_areaname";
            this.comboBox1.ValueMember = "db_areaname";
            con.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            //MySqlCommand setchar = new MySqlCommand(@"set names gb2312", con);
            //setchar.ExecuteNonQuery();
            string mysql = "SELECT  ctr_name  FROM  run_ctr where ctr_name='" + myshouche.Name + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
            //string mysql = "SELECT   COUNT( txt_name='" + mytext.Name + "') FROM  run_temp ";

            MySqlCommand cmd = new MySqlCommand(mysql, con);
            object count = cmd.ExecuteScalar();

            string sql;

            if (count == null)

                sql = "INSERT INTO run_ctr(faultname,ctr_name,db_name,ctr_kind,project) VALUES('" + comboBox3.Text + "', '" + textBox1.Text + "' ,'" + comboBox2.Text + "','K50','" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "')";
            //sql = "INSERT INTO run_ctr(ctr_name,db_name,ctr_kind) VALUES( '" + textBox1.Text + "' ,'" + kind + "','" + kind + "')";
            else
                sql = "update run_ctr set db_name='" + comboBox2.Text + "',ctr_name='" + textBox1.Text + "',faultname='" + comboBox3.Text + "' where ctr_name='" + myshouche.Name + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";

            MySqlCommand mInsertCmd = new MySqlCommand(sql, con);
            mInsertCmd.CommandType = CommandType.Text;
            mInsertCmd.ExecuteNonQuery();
            con.Close();
            myshouche.Name = this.textBox1.Text;
            myshouche.Bol = this.textBox2.Text;
            myshouche.FaultSignal = this.textBox3.Text;
            this.myshouche.Refresh();
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            string sql = "select db_name from dbpoint where db_areaname='" + comboBox1.Text + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";

            MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);

            DataSet ds1 = new DataSet();
            DataSet ds2 = new DataSet();

            oda.Fill(ds1, "dbpoint");
            oda.Fill(ds2, "dbpoint");
            this.comboBox3.DataSource = ds1.Tables["dbpoint"];
            this.comboBox3.DisplayMember = "db_name";
            this.comboBox3.ValueMember = "db_name";
            this.comboBox2.DataSource = ds2.Tables["dbpoint"];
            this.comboBox2.DisplayMember = "db_name";
            this.comboBox2.ValueMember = "db_name";

            con.Close();
        }


    }
}